package com.gewu.bookeeping;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.gewu.bookeeping.View.fragment.fourFragment;
import com.gewu.bookeeping.View.fragment.oneFragment;
import com.gewu.bookeeping.View.fragment.thrFragemnt;
import com.gewu.bookeeping.View.fragment.twoFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.List;

public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener{

    private FrameLayout mainContent;
    private BottomNavigationView mainNavigation;
    private com.gewu.bookeeping.View.fragment.oneFragment oneFragment = new oneFragment();
    private com.gewu.bookeeping.View.fragment.twoFragment twoFragment = new twoFragment();
    private com.gewu.bookeeping.View.fragment.thrFragemnt thrFragemnt = new thrFragemnt();
    private com.gewu.bookeeping.View.fragment.fourFragment fourFragment = new fourFragment();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initFragment();
    }

    /**
     * 初始化fragment
     */
    private void initFragment() {
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.mainContent, oneFragment).add(R.id.mainContent, twoFragment).add(R.id.mainContent, thrFragemnt)
                .add(R.id.mainContent, fourFragment).hide(twoFragment).hide(thrFragemnt).hide(fourFragment).show(oneFragment).commit();
    }

    /**
     * 初始化布局
     */
    private void initView() {
        mainContent = (FrameLayout) findViewById(R.id.mainContent);
        mainNavigation = (BottomNavigationView) findViewById(R.id.mainNavigation);
        mainNavigation.setOnNavigationItemSelectedListener(this);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case R.id.IncomeExpenditure:
                showFragment(oneFragment);
                break;
            case R.id.Reckoning:
                showFragment(twoFragment);
                break;
            case R.id.Collection:
                showFragment(thrFragemnt);
                break;
            case R.id.PersonalCenter:
                showFragment(fourFragment);
                break;
        }
        return true;
    }

    /**
     * 显示fragment
     * @param fragment
     */
    private void showFragment(Fragment fragment) {
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        List<Fragment> fragments = supportFragmentManager.getFragments();
        for (Fragment f:fragments){
            fragmentTransaction.hide(f);
        }
        fragmentTransaction.show(fragment).commit();
    }
}